package SpringFramework.异步任务.config;

import java.util.concurrent.Executor;

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

// @Configuration声明当前类是一个配置类
@Configuration
// 使用@ComponentScan，自动扫描包名下所有使用@Component、@Service、@Repository和@Controller的类，并注册为Bean。
@ComponentScan("zzz.example.Spring.异步任务")
// 开启异步任务支持
@EnableAsync
public class JavaConfig implements AsyncConfigurer {

	// 配置类实现AsyncConfigurer接口病重写getAsyncExecutor方法，并返回一个ThreadPoolTaskExecutor
	public Executor getAsyncExecutor() {
		ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
		executor.setCorePoolSize(5);
		executor.setMaxPoolSize(10);
		executor.setQueueCapacity(25);
		executor.initialize();
		return executor;
	}

	@Override
	public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
		return null;
	}
	
}
